﻿using System;
using WeShare.WeiXin.Common;
using WeShare.WeiXin.Common.EnumKey;

namespace WeShare.WeiXin.Mp.JsSdk.Entity
{
    /// <summary>
    ///调用jssdk的参数
    /// </summary>
    public class WxJsParam
    {
        private WxJsParam() { }
        public WxJsParam(string appid, string url, bool debug = false)
        {
            this.url = url;
            this.debug = debug;
            this.appId = appid;
            //将字段添加到列表中
            string[] arr = new[]
            {
                $"noncestr={this.nonceStr}",
                $"jsapi_ticket={ApiTicket.GetValue(TicketType.jsapi,appid)}",
                $"timestamp={this.timestamp}",
                $"url={url}"
            };
            //字典排序
            Array.Sort(arr);
            //使用URL键值对的格式拼接成字符串
            var temp = string.Join("&", arr);
            this.signature = Utils.Sha1(temp);
        }
        /// <summary>
        /// 时间戳
        /// </summary>
        public int timestamp { get; set; } = Utils.GetTimeStamp();

        /// <summary>
        /// 随机字符串
        /// </summary>
        public string nonceStr { get; set; } = Utils.GetTimeStamp().ToString();
        /// <summary>
        /// 当前页面的url
        /// </summary>
        public string url { get; set; }
        /// <summary>
        /// 签名
        /// </summary>
        public string signature { get; set; }
        /// <summary>
        /// 是否开启调试模式
        /// </summary>
        public bool debug { get; set; }
        /// <summary>
        /// 公众号ID
        /// </summary>
        public string appId { get; set; }
    }
}